<?php


namespace App\Services;


use App\Constants\AdvConfigConstant;
use App\Constants\MemberTaskConstant;
use App\Constants\MiniProgramConstant;
use App\Constants\SignConstant;
use App\Models\MemberTask;
use App\Models\Settings;
use EasyWeChat\Factory;

class MiniProgramService
{
    public function initSetting(string $type)
    {
        $memberTaskArr = [];
        $settingArr = [];

        if (!MemberTask::where('mini_type', $type)->exists()) {
            foreach (MemberTaskConstant::MAP as $key => $item) {
                $memberTaskArr[] = [
                    'slug'       => $key,
                    'remark'     => $item,
                    'mini_type'  => $type,
                    'created_at' => date('Y-m-d H:i:s'),
                    'updated_at' => date('Y-m-d H:i:s'),
                ];
            }
            MemberTask::insert($memberTaskArr);
        }

        if (!Settings::where('mini_type', $type)->exists()) {
            foreach (AdvConfigConstant::MAP as $name => $remark) {
                $settingArr[] = [
                    'name'       => $name,
                    'group'      => AdvConfigConstant::ADV_GROUP,
                    'remark'     => $remark,
                    'mini_type'  => $type,
                    'created_at' => date('Y-m-d H:i:s'),
                    'updated_at' => date('Y-m-d H:i:s'),
                ];
            }

            foreach (SignConstant::MAP as $name => $remark) {
                $settingArr[] = [
                    'name'       => $name,
                    'group'      => SignConstant::GROUP,
                    'remark'     => $remark,
                    'mini_type'  => $type,
                    'created_at' => date('Y-m-d H:i:s'),
                    'updated_at' => date('Y-m-d H:i:s'),
                ];
            }

            foreach (MiniProgramConstant::SETTING_MAP as $name => $remark) {
                $settingArr[] = [
                    'name'       => $name,
                    'group'      => MiniProgramConstant::GROUP,
                    'remark'     => $remark,
                    'mini_type'  => $type,
                    'created_at' => date('Y-m-d H:i:s'),
                    'updated_at' => date('Y-m-d H:i:s'),
                ];
            }

            Settings::insert($settingArr);
        }
    }

    public function deleteSetting(array $type)
    {
        MemberTask::whereIn('mini_type', $type)->delete();
        Settings::whereIn('mini_type', $type)->delete();
    }

    /**
     * 生成小程序短链
     * @param string $miniType
     * @param string $path
     * @param string $title
     * @param bool $isPermanent
     * @return array
     */
    public function getShortLink(string $miniType, string $path, string $title = '', bool $isPermanent = false): array
    {
        try {
            $settings = getMiniSettings($miniType, MiniProgramConstant::GROUP);
            $app = Factory::miniProgram(array_merge([
                'app_id' => $settings[MiniProgramConstant::APP_ID],
                'secret' => $settings[MiniProgramConstant::APP_SECRET],
            ], config('wechat.defaults')));

            $ret = $app->short_link->getShortLink($path, $title, $isPermanent);

            $link = $ret['link'] ?? null;

            if ($link) return resultSuccessReturn('success', $link);

            return resultFailedReturn($ret['errmsg'] ?? '获取小程序短链失败');
        } catch (\Throwable $e) {
            log_exception('获取小程序短链异常', $e);
            return resultFailedReturn($e->getMessage());
        }
    }
}
